package com.naughty.shopapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.naughty.shopapi.mapper.AddressMapper;
import com.naughty.shopapi.mapper.UsersMapper;
import com.naughty.shopapi.pojo.Address;
import com.naughty.shopapi.pojo.RegisterErrors;
import com.naughty.shopapi.pojo.Users;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.naughty.shopapi.utils.JWTUtil.isExpiration;

@Api(tags = "收货地址操作")
@RestController
@RequestMapping("api")
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private UsersMapper usersMapper;


    private static Pattern pattern; //Pattern是用于编译正则表达式
    private static Matcher matcher; //Matcher用于匹配正则表达式

    /**
     * 添加地址
     *
     * @param name       收货人姓名
     * @param address    详细地址
     * @param phone      收货手机
     * @param province   省份
     * @param city       城市
     * @param county     区县
     * @param is_default 是否默认，值为1是默认，不传就不设置默认，注意数据库中只能存在一个默认
     * @return
     */
    @ApiOperation("新增地址")
    @PostMapping("/address")
    public ResponseEntity<Object> insertAddress(
            @RequestParam String name,
            @RequestParam String address,
            @RequestParam String phone,
            @RequestParam String province,
            @RequestParam String city,
            @RequestParam String county,
            @RequestParam(defaultValue = "0") Integer is_default
    ) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        final String Authorization = request.getHeader("authorization");
        String[] tokens = Authorization.split(" ");
        String token = tokens[1];
        final HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("remember_token", token);
        final List<Users> isToken = usersMapper.selectByMap(tokenMap);
        if (isToken.size() == 0) {
            final RegisterErrors registerErrors = new RegisterErrors();
            registerErrors.setMessage("The token has been blacklisted.");
            registerErrors.setErrors("Token已被列入黑名单");
            registerErrors.setStatus_code(401);
            return new ResponseEntity<>(registerErrors, HttpStatus.UNAUTHORIZED);
        } else {
            if (!isExpiration(token)) {//判断token有效期
                final RegisterErrors registerErrors = new RegisterErrors();
                final HashMap<String, Object> errormsg = new HashMap<>();
                final ArrayList<Object> nameMsg = new ArrayList<>();
                final ArrayList<Object> addressMsg = new ArrayList<>();
                final ArrayList<Object> phoneMsg = new ArrayList<>();
                final ArrayList<Object> provinceMsg = new ArrayList<>();
                final ArrayList<Object> cityMsg = new ArrayList<>();
                final ArrayList<Object> countyMsg = new ArrayList<>();

                //必填条件判断状态
                Boolean nameBoo = false, addressBoo = false, phoneBoo = false,
                        provinceBoo = false, cityBoo = false, countyBoo = false, iDefaultBoo = false;
                /**
                 * 参数条件：
                 * name：非空，
                 * address：非空，
                 * phone：非空，电话格式，
                 * province，非空
                 * city：非空，
                 * county：非空
                 */

                if (name == "") {//name 条件
                    nameMsg.add("收货人 不能为空");
                    errormsg.put("name", nameMsg);
                } else {
                    nameBoo = true;
                }

                if (address == "") {//name 条件
                    addressMsg.add("收货人 不能为空");
                    errormsg.put("address", addressMsg);
                } else {
                    addressBoo = true;
                }

                /**
                 * 手机号:目前全国有27种手机号段。
                 * 移动有16个号段：134、135、136、137、138、139、147、150、151、152、157、158、159、182、187、188。其中147、157、188是3G号段，其他都是2G号段。
                 * 联通有7种号段：130、131、132、155、156、185、186。其中186是3G（WCDMA）号段，其余为2G号段。
                 * 电信有4个号段：133、153、180、189。其中189是3G号段（CDMA2000），133号段主要用作无线网卡号。
                 * 150、151、152、153、155、156、157、158、159 九个;
                 * 130、131、132、133、134、135、136、137、138、139 十个;
                 * 180、182、185、186、187、188、189 七个;
                 * 13、15、18三个号段共30个号段，154、181、183、184暂时没有，加上147共27个。
                 */
                pattern = Pattern.compile("^((13\\d{9}$)|(15[0,1,2,3,5,6,7,8,9]\\d{8}$)|(18[0,2,5,6,7,8,9]\\d{8}$)|(147\\d{8})$)");

                if (phone == "") {//phone 条件
                    phoneMsg.add("手机号 不能为空");
                    errormsg.put("phone", phoneMsg);
                } else {
                    matcher = pattern.matcher(phone);
                    if (matcher.matches()) {
                        //电话格式合法
                        phoneBoo = true;
                    } else {
                        phoneMsg.add("电话 格式不正确");
                        errormsg.put("phone", phoneMsg);
                    }
                }

                if (province == "") {//province 条件
                    provinceMsg.add("省份 不能为空");
                    errormsg.put("province", provinceMsg);
                } else {
                    provinceBoo = true;
                }

                if (city == "") {//city 条件
                    cityMsg.add("城市 不能为空");
                    errormsg.put("city", cityMsg);
                } else {
                    cityBoo = true;
                }

                if (county == "") {//county 条件
                    countyMsg.add("区县 不能为空");
                    errormsg.put("city", countyMsg);
                } else {
                    countyBoo = true;
                }
                if (is_default == 1) {//is_default 条件
                    iDefaultBoo = true;
                }

                if (nameBoo && addressBoo && phoneBoo && provinceBoo && cityBoo && countyBoo) {
                    final Address addressInfo = new Address();
                    addressInfo.setUser_id(isToken.get(0).getId());
                    addressInfo.setName(name);
                    addressInfo.setProvince(province);
                    addressInfo.setCity(city);
                    addressInfo.setCounty(county);
                    addressInfo.setAddress(address);
                    addressInfo.setPhone(phone);
                    if (iDefaultBoo) {//当is_default=1时，先将该用户所有地址is_default字段默认归0，之后在进行插入新默认地址
                        addressInfo.setIs_default(is_default);
                        UpdateWrapper<Address> updateWrapperDef = new UpdateWrapper<>();
                        updateWrapperDef.eq("user_id", isToken.get(0).getId()).set("is_default", 0);
                        addressMapper.update(null, updateWrapperDef);
                    }
                    addressMapper.insert(addressInfo);
                    return new ResponseEntity<>(null, HttpStatus.CREATED);
                } else {
                    registerErrors.setMessage("The given data was invalid.");
                    registerErrors.setErrors(errormsg);
                    registerErrors.setStatus_code(422);
                    return new ResponseEntity<>(registerErrors, HttpStatus.UNPROCESSABLE_ENTITY);
                }
            } else {
                final RegisterErrors registerErrors = new RegisterErrors();
                registerErrors.setMessage("Token Signature could not be verified.");
                registerErrors.setErrors("Token已失效");
                registerErrors.setStatus_code(401);
                return new ResponseEntity<>(registerErrors, HttpStatus.UNAUTHORIZED);
            }
        }
    }

    /**
     * 地址列表
     *
     * @return
     */
    @ApiOperation("获取地址列表")
    @GetMapping("/address")
    public ResponseEntity<Object> getAddressList() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        final String Authorization = request.getHeader("authorization");
        String[] tokens = Authorization.split(" ");
        String token = tokens[1];
        final HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("remember_token", token);
        final List<Users> isToken = usersMapper.selectByMap(tokenMap);
        if (isToken.size() == 0) {
            final RegisterErrors registerErrors = new RegisterErrors();
            registerErrors.setMessage("The token has been blacklisted.");
            registerErrors.setErrors("Token已被列入黑名单");
            registerErrors.setStatus_code(401);
            return new ResponseEntity<>(registerErrors, HttpStatus.UNAUTHORIZED);
        } else {
            if (!isExpiration(token)) {//判断token有效期
                final HashMap<String, Object> dataMap = new HashMap<>();
                final QueryWrapper<Address> wrapperAddressList = new QueryWrapper<>();
                wrapperAddressList.eq("user_id", isToken.get(0).getId());
                dataMap.put("data", addressMapper.selectList(wrapperAddressList));
                return new ResponseEntity<>(dataMap, HttpStatus.OK);
            } else {
                final RegisterErrors registerErrors = new RegisterErrors();
                registerErrors.setMessage("Token Signature could not be verified.");
                registerErrors.setErrors("Token已失效");
                registerErrors.setStatus_code(401);
                return new ResponseEntity<>(registerErrors, HttpStatus.UNAUTHORIZED);
            }
        }
    }

    /**
     * 地址详情
     *
     * @param address 地址ID
     * @return
     */
    @ApiOperation("查询地址详情")
    @GetMapping("/address/{address}")
    public ResponseEntity<Object> getAddressDetail(@PathVariable("address") Integer address) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        final String Authorization = request.getHeader("authorization");
        String[] tokens = Authorization.split(" ");
        String token = tokens[1];
        final HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("remember_token", token);
        final List<Users> isToken = usersMapper.selectByMap(tokenMap);
        if (isToken.size() == 0) {
            final RegisterErrors registerErrors = new RegisterErrors();
            registerErrors.setMessage("The token has been blacklisted.");
            registerErrors.setErrors("Token已被列入黑名单");
            registerErrors.setStatus_code(401);
            return new ResponseEntity<>(registerErrors, HttpStatus.UNAUTHORIZED);
        } else {
            if (!isExpiration(token)) {//判断token有效期
                final HashMap<String, Object> dataMap = new HashMap<>();
                final QueryWrapper<Address> wrapperAddressDetail = new QueryWrapper<>();
                wrapperAddressDetail.eq("user_id", isToken.get(0).getId());
                wrapperAddressDetail.eq("id", address);
                final List<Address> addresses = addressMapper.selectList(wrapperAddressDetail);
                if (addresses.size() == 0) {
                    dataMap.put("message", "只能查看自己的地址");
                    dataMap.put("status_code", 400);
                    return new ResponseEntity<>(dataMap, HttpStatus.BAD_REQUEST);
                } else {
                    final Address addressDetail = new Address();
                    addressDetail.setId(addresses.get(0).getId());
                    addressDetail.setUser_id(addresses.get(0).getUser_id());
                    addressDetail.setName(addresses.get(0).getName());
                    addressDetail.setProvince(addresses.get(0).getProvince());
                    addressDetail.setCity(addresses.get(0).getCity());
                    addressDetail.setCounty(addresses.get(0).getCounty());
                    addressDetail.setAddress(addresses.get(0).getAddress());
                    addressDetail.setPhone(addresses.get(0).getPhone());
                    addressDetail.setIs_default(addresses.get(0).getIs_default());
                    addressDetail.setShow(addresses.get(0).getShow());
                    addressDetail.setCreated_at(addresses.get(0).getCreated_at());
                    addressDetail.setUpdated_at(addresses.get(0).getUpdated_at());
                    return new ResponseEntity<>(addressDetail, HttpStatus.OK);
                }
            } else {
                final RegisterErrors registerErrors = new RegisterErrors();
                registerErrors.setMessage("Token Signature could not be verified.");
                registerErrors.setErrors("Token已失效");
                registerErrors.setStatus_code(401);
                return new ResponseEntity<>(registerErrors, HttpStatus.UNAUTHORIZED);
            }
        }
    }

    /**
     * 更新地址
     *
     * @param addressId  地址ID
     * @param name       收货人姓名
     * @param address    详细地址
     * @param phone      收货手机
     * @param province   省份
     * @param city       城市
     * @param county     区县
     * @param is_default 是否默认，值为1是默认，不传就不设置默认
     * @return
     */
    @ApiOperation("更新地址")
    @PutMapping("/address/{addressId}")
    public ResponseEntity<Object> alterAddress(
            @PathVariable("addressId") Integer addressId,
            @RequestParam String name,
            @RequestParam String address,
            @RequestParam String phone,
            @RequestParam String province,
            @RequestParam String city,
            @RequestParam String county,
            @RequestParam(defaultValue = "99999") Integer is_default
    ) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        final String Authorization = request.getHeader("authorization");
        String[] tokens = Authorization.split(" ");
        String token = tokens[1];
        final HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("remember_token", token);
        final List<Users> isToken = usersMapper.selectByMap(tokenMap);
        if (isToken.size() == 0) {
            final RegisterErrors registerErrors = new RegisterErrors();
            registerErrors.setMessage("The token has been blacklisted.");
            registerErrors.setErrors("Token已被列入黑名单");
            registerErrors.setStatus_code(401);
            return new ResponseEntity<>(registerErrors, HttpStatus.UNAUTHORIZED);
        } else {
            if (!isExpiration(token)) {//判断token有效期
                final RegisterErrors registerErrors = new RegisterErrors();
                final HashMap<String, Object> errormsg = new HashMap<>();
                final ArrayList<Object> nameMsg = new ArrayList<>();
                final ArrayList<Object> addressMsg = new ArrayList<>();
                final ArrayList<Object> phoneMsg = new ArrayList<>();
                final ArrayList<Object> provinceMsg = new ArrayList<>();
                final ArrayList<Object> cityMsg = new ArrayList<>();
                final ArrayList<Object> countyMsg = new ArrayList<>();
                final ArrayList<Object> addressIdMsg = new ArrayList<>();

                //必填条件判断状态
                Boolean nameBoo = false, addressBoo = false, phoneBoo = false, addressIdBoo = false,
                        provinceBoo = false, cityBoo = false, countyBoo = false, iDefaultBoo = false,notDefaultBoo = false;
                /**
                 * 参数条件：
                 * addressId：当前用户存在此地址id
                 * name：非空，
                 * address：非空，
                 * phone：非空，电话格式，
                 * province，非空
                 * city：非空，
                 * county：非空
                 */

                //addressId查实操作
                final QueryWrapper<Address> wrapperAddress = new QueryWrapper<>();
                wrapperAddress.eq("user_id", isToken.get(0).getId());
                wrapperAddress.eq("id", addressId);
                final List<Address> isAddress = addressMapper.selectList(wrapperAddress);
                if (isAddress.size() == 0) {//cart 条件
                    addressIdMsg.add("地址ID 找不到");
                    errormsg.put("address", addressIdMsg);
                } else {
                    addressIdBoo = true;
                }

                if (name == "") {//name 条件
                    nameMsg.add("收货人 不能为空");
                    errormsg.put("name", nameMsg);
                } else {
                    nameBoo = true;
                }

                if (address == "") {//name 条件
                    addressMsg.add("收货人 不能为空");
                    errormsg.put("address", addressMsg);
                } else {
                    addressBoo = true;
                }

                /**
                 * 手机号:目前全国有27种手机号段。
                 * 移动有16个号段：134、135、136、137、138、139、147、150、151、152、157、158、159、182、187、188。其中147、157、188是3G号段，其他都是2G号段。
                 * 联通有7种号段：130、131、132、155、156、185、186。其中186是3G（WCDMA）号段，其余为2G号段。
                 * 电信有4个号段：133、153、180、189。其中189是3G号段（CDMA2000），133号段主要用作无线网卡号。
                 * 150、151、152、153、155、156、157、158、159 九个;
                 * 130、131、132、133、134、135、136、137、138、139 十个;
                 * 180、182、185、186、187、188、189 七个;
                 * 13、15、18三个号段共30个号段，154、181、183、184暂时没有，加上147共27个。
                 */
                pattern = Pattern.compile("^((13\\d{9}$)|(15[0,1,2,3,5,6,7,8,9]\\d{8}$)|(18[0,2,5,6,7,8,9]\\d{8}$)|(147\\d{8})$)");

                if (phone == "") {//phone 条件
                    phoneMsg.add("手机号 不能为空");
                    errormsg.put("phone", phoneMsg);
                } else {
                    matcher = pattern.matcher(phone);
                    if (matcher.matches()) {
                        //电话格式合法
                        phoneBoo = true;
                    } else {
                        phoneMsg.add("电话 格式不正确");
                        errormsg.put("phone", phoneMsg);
                    }
                }

                if (province == "") {//province 条件
                    provinceMsg.add("省份 不能为空");
                    errormsg.put("province", provinceMsg);
                } else {
                    provinceBoo = true;
                }

                if (city == "") {//city 条件
                    cityMsg.add("城市 不能为空");
                    errormsg.put("city", cityMsg);
                } else {
                    cityBoo = true;
                }

                if (county == "") {//county 条件
                    countyMsg.add("区县 不能为空");
                    errormsg.put("city", countyMsg);
                } else {
                    countyBoo = true;
                }
                if (is_default == 1) {//is_default 条件
                    iDefaultBoo = true;
                }
                if (is_default == 0){
                    notDefaultBoo = true;
                }
                if (nameBoo && addressBoo && phoneBoo && provinceBoo && cityBoo && countyBoo && addressIdBoo) {
                    UpdateWrapper<Address> updateAddressWrapper = new UpdateWrapper<>();
                    updateAddressWrapper.eq("id", addressId)
                            .set("name", name)
                            .set("address", address)
                            .set("phone", phone)
                            .set("province", province)
                            .set("city", city)
                            .set("county", county);

                    UpdateWrapper<Address> updateWrapperDef = new UpdateWrapper<>();//全部收货地址列
                    updateWrapperDef.eq("user_id", isToken.get(0).getId()).set("is_default", 0);

                    if (iDefaultBoo) {//当is_default=1时，先将该用户所有地址is_default字段默认归0，之后在进行插入新默认地址
                        updateAddressWrapper.eq("id", addressId)
                                .set("is_default", 1);
                        addressMapper.update(null, updateWrapperDef);

                    }
                    if (notDefaultBoo) {//当is_default=1时，先将该用户所有地址is_default字段默认归0，之后在进行插入新默认地址
                        updateAddressWrapper.eq("id", addressId)
                                .set("is_default", 0);
                        addressMapper.update(null, updateWrapperDef);
                    }
                    addressMapper.update(null, updateAddressWrapper);
                    return new ResponseEntity<>(null, HttpStatus.NO_CONTENT);
                } else {
                    registerErrors.setMessage("The given data was invalid.");
                    registerErrors.setErrors(errormsg);
                    registerErrors.setStatus_code(422);
                    return new ResponseEntity<>(registerErrors, HttpStatus.UNPROCESSABLE_ENTITY);
                }
            } else {
                final RegisterErrors registerErrors = new RegisterErrors();
                registerErrors.setMessage("Token Signature could not be verified.");
                registerErrors.setErrors("Token已失效");
                registerErrors.setStatus_code(401);
                return new ResponseEntity<>(registerErrors, HttpStatus.UNAUTHORIZED);
            }
        }
    }

    /**
     * 删除地址
     *
     * @param address 地址ID
     * @return
     */
    @ApiOperation("删除地址")
    @DeleteMapping("/address/{address}")
    public ResponseEntity<Object> deleteAddress(@PathVariable("address") Integer address) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        final String Authorization = request.getHeader("authorization");
        String[] tokens = Authorization.split(" ");
        String token = tokens[1];
        final HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("remember_token", token);
        final List<Users> isToken = usersMapper.selectByMap(tokenMap);
        if (isToken.size() == 0) {
            final RegisterErrors registerErrors = new RegisterErrors();
            registerErrors.setMessage("The token has been blacklisted.");
            registerErrors.setErrors("Token已被列入黑名单");
            registerErrors.setStatus_code(401);
            return new ResponseEntity<>(registerErrors, HttpStatus.UNAUTHORIZED);
        } else {
            if (!isExpiration(token)) {//判断token有效期
                final RegisterErrors registerErrors = new RegisterErrors();
                final HashMap<String, Object> errormsg = new HashMap<>();
                final ArrayList<Object> addressIdMsg = new ArrayList<>();

                Boolean addressIdBoo = false;
                //addressId查实操作
                final QueryWrapper<Address> wrapperAddress = new QueryWrapper<>();
                wrapperAddress.eq("user_id", isToken.get(0).getId());
                wrapperAddress.eq("id", address);
                final List<Address> isAddress = addressMapper.selectList(wrapperAddress);
                if (isAddress.size() == 0) {//cart 条件
                    addressIdMsg.add("地址ID 找不到");
                    errormsg.put("address", addressIdMsg);
                } else {
                    addressIdBoo = true;
                }
                if (addressIdBoo) {
                    UpdateWrapper<Address> deleteWrapperAddress = new UpdateWrapper<>();
                    deleteWrapperAddress.eq("user_id", isToken.get(0).getId())
                            .eq("id", address);
                    addressMapper.delete(deleteWrapperAddress);
                    return new ResponseEntity<>(null, HttpStatus.NO_CONTENT);
                } else {
                    registerErrors.setMessage("The given data was invalid.");
                    registerErrors.setErrors(errormsg);
                    registerErrors.setStatus_code(422);
                    return new ResponseEntity<>(registerErrors, HttpStatus.UNPROCESSABLE_ENTITY);
                }
            } else {
                final RegisterErrors registerErrors = new RegisterErrors();
                registerErrors.setMessage("Token Signature could not be verified.");
                registerErrors.setErrors("Token已失效");
                registerErrors.setStatus_code(401);
                return new ResponseEntity<>(registerErrors, HttpStatus.UNAUTHORIZED);
            }
        }
    }

    /**
     * 设置为默认地址
     *
     * @param address 地址ID
     * @return
     */
    @ApiOperation("设置默认地址")
    @PostMapping("/address/{address}/default")
    public ResponseEntity<Object> setDefaultAddress(@PathVariable("address") Integer address) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        final String Authorization = request.getHeader("authorization");
        String[] tokens = Authorization.split(" ");
        String token = tokens[1];
        final HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("remember_token", token);
        final List<Users> isToken = usersMapper.selectByMap(tokenMap);
        if (isToken.size() == 0) {
            final RegisterErrors registerErrors = new RegisterErrors();
            registerErrors.setMessage("The token has been blacklisted.");
            registerErrors.setErrors("Token已被列入黑名单");
            registerErrors.setStatus_code(401);
            return new ResponseEntity<>(registerErrors, HttpStatus.UNAUTHORIZED);
        } else {
            if (!isExpiration(token)) {//判断token有效期
                final RegisterErrors registerErrors = new RegisterErrors();
                final HashMap<String, Object> errormsg = new HashMap<>();
                final ArrayList<Object> addressIdMsg = new ArrayList<>();

                Boolean addressIdBoo = false;
                //addressId查实操作
                final QueryWrapper<Address> wrapperAddress = new QueryWrapper<>();
                wrapperAddress.eq("user_id", isToken.get(0).getId());
                wrapperAddress.eq("id", address);
                final List<Address> isAddress = addressMapper.selectList(wrapperAddress);
                if (isAddress.size() == 0) {//cart 条件
                    addressIdMsg.add("地址ID 找不到");
                    errormsg.put("address", addressIdMsg);
                } else {
                    addressIdBoo = true;
                }
                if (addressIdBoo) {
                    //将该用户所有的地址is_default字段值设为0
                    UpdateWrapper<Address> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("user_id", isToken.get(0).getId()).set("is_default", 0);
                    addressMapper.update(null, updateWrapper);

                    //将该用户要设定为默认值的地址is_default字段值设为1
                    UpdateWrapper<Address> updateAddressWrapper = new UpdateWrapper<>();
                    updateAddressWrapper.eq("id", address)
                            .eq("user_id", isToken.get(0).getId())
                            .set("is_default", 1);
                    addressMapper.update(null, updateAddressWrapper);
                    return new ResponseEntity<>(null, HttpStatus.NO_CONTENT);
                } else {
                    registerErrors.setMessage("The given data was invalid.");
                    registerErrors.setErrors(errormsg);
                    registerErrors.setStatus_code(422);
                    return new ResponseEntity<>(registerErrors, HttpStatus.UNPROCESSABLE_ENTITY);
                }
            } else {
                final RegisterErrors registerErrors = new RegisterErrors();
                registerErrors.setMessage("Token Signature could not be verified.");
                registerErrors.setErrors("Token已失效");
                registerErrors.setStatus_code(401);
                return new ResponseEntity<>(registerErrors, HttpStatus.UNAUTHORIZED);
            }
        }
    }
}
